package com.wuyou.mid;

import sun.reflect.generics.tree.Tree;

import java.util.*;

/**
 * 652. 寻找重复的子树
 */
public class FindDuplicateSubtrees {

    public static void main(String[] args) {

        String str = "2,4,";
        System.out.println(str.split(",").length);
    }

    public List<TreeNode> solution_1(TreeNode root) {
        countNodeVal(root);
        return resList;
    }


    List<TreeNode> resList = new ArrayList<>();
    Map<String, Integer> map = new HashMap<String, Integer>();

    public String countNodeVal(TreeNode root) {
        String str = "";
        if (root != null) {
            str = root.val + "," + countNodeVal(root.left) + "," + countNodeVal(root.right);
        }
        if (map.getOrDefault(str, 0) == 1) resList.add(root);

        map.put(str, map.getOrDefault(str, 0) + 1);
        return str;
    }

}
